package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.ProductMapper;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductDao productDao;
    @Autowired
    ProducttDao producttDao;
    @Autowired
    AttributeValueDao attributeValueDao;
    @Autowired
    ChanpinShuxinDao chanpinShuxinDao;
    @Autowired
    ChanpincongbiaoShuxinzhiDao chanpincongbiaoShuxinzhiDao;

    //产品前端分页
    public List<Productt> selectProduct(String search,String categoryName){
        return productMapper.selectProduct(search,categoryName);
    }

    //查询类别集合
    public List<Category> selectCategory(){
        return productMapper.selectCategory();
    }

    public void addProduct(Product product, Productt productt, List<Integer> guige,List<Integer> attrid,String imgs) {
        System.out.println("service"+imgs);
         if(product.getProductId()==0){
            for(int i=0;i<productMapper.selectProName().size();i++){
                System.out.println(productMapper.selectProName().get(i));
                if(product.getProductName().equals(productMapper.selectProName().get(i).getProductName())){
                    System.out.println("数据库"+productMapper.selectProName().get(i).getProductName());
                    System.out.println("前端"+product.getProductName());
                    //不新增产品主表
                    //产品id
                    int id = productMapper.selectProName().get(i).getProductId();
                    System.out.println("主表不新增");
                    productt.setProduct(productMapper.selectProName().get(i));
                    if(imgs!=null){
                        productt.setProductImgs(imgs);
                    }
                    producttDao.save(productt);
                    for (int x = 0; x < productMapper.SelectShuXin(id).size(); x++) {
                        for(int j=0;j<attrid.size();j++) {
                            if (attrid.get(j) == productMapper.SelectShuXin(id).get(x).getAttributeId().getAttributeId()){
                                attrid.remove(attrid.get(j));
                            }
                        }
                    }
                    //产品属性中间表新增
                    Attribute attribute = null;
                    for(int z=0;z<attrid.size();z++) {
                        attribute = new Attribute();
                        attribute.setAttributeId(attrid.get(z));
                        ChanpinShuxin chanpinShuxin = new ChanpinShuxin();
                        chanpinShuxin.setAttributeId(attribute);
                        chanpinShuxin.setProductId(productMapper.selectProName().get(i));
                        chanpinShuxinDao.save(chanpinShuxin);
                    }
                    AttributeValue attributeValue = null;
                    for(int x=0;x<guige.size();x++){
                        attributeValue = new AttributeValue();
                        attributeValue.setAttributevId(guige.get(x));
                        ChanpincongbiaoShuxinzhi cp = new ChanpincongbiaoShuxinzhi();
                        cp.setProductts(productt);
                        cp.setAttributevId(attributeValue);
                        chanpincongbiaoShuxinzhiDao.save(cp);
                    }
                    return;
                }else if(i==productMapper.selectProName().size()-1){
                     //新增
                    productDao.save(product);
                    productt.setProduct(product);
                    if(imgs!=null){
                        productt.setProductImgs(imgs);
                    }
                    producttDao.save(productt);
                    Attribute attribute = null;
                    for(int j=0;j<attrid.size();j++){
                        attribute = new Attribute();
                        attribute.setAttributeId(attrid.get(j));
                        ChanpinShuxin chanpinShuxin = new ChanpinShuxin();
                        chanpinShuxin.setAttributeId(attribute);
                        chanpinShuxin.setProductId(product);
                        chanpinShuxinDao.save(chanpinShuxin);
                    }
                    AttributeValue attributeValue = null;
                    for(int x=0;x<guige.size();x++){
                        attributeValue = new AttributeValue();
                        attributeValue.setAttributevId(guige.get(x));
                        ChanpincongbiaoShuxinzhi cp = new ChanpincongbiaoShuxinzhi();
                        cp.setProductts(productt);
                        cp.setAttributevId(attributeValue);
                        chanpincongbiaoShuxinzhiDao.save(cp);
                    }
                    return;
                }
            }
        }else{
            //修改
            System.out.println("修改");
            productDao.save(product);
            productt.setProduct(product);
             if(imgs!=null){
                 productt.setProductImgs(imgs);
             }else{
                 productt.setProductImgs(productt.getProductImgs());
             }
            producttDao.save(productt);
          /*  for (int x = 0; x < productMapper.SelectShuXin(product.getProductId()).size(); x++) {
                for(int j=0;j<attrid.size();j++) {
                    if (attrid.get(j) == productMapper.SelectShuXin(product.getProductId()).get(x).getAttributeId().getAttributeId()){
                        attrid.remove(attrid.get(j));
                    }
                }
            }*/
           /* System.out.println(attrid);
            Attribute attribute = null;
            for(int j=0;j<attrid.size();j++){
                attribute = new Attribute();
                attribute.setAttributeId(attrid.get(j));
                ChanpinShuxin chanpinShuxin = new ChanpinShuxin();
                chanpinShuxin.setAttributeId(attribute);
                chanpinShuxin.setProductId(product);
            }
            AttributeValue attributeValue = null;
            for(int x=0;x<guige.size();x++){
                attributeValue = new AttributeValue();
                attributeValue.setAttributevId(guige.get(x));
                ChanpincongbiaoShuxinzhi cp = new ChanpincongbiaoShuxinzhi();
                cp.setProductts(productt);
                cp.setAttributevId(attributeValue);
            }*/
        }
    }

    //查询需要修改的产品
    public List<Productt> upProduct(Integer productId) {
        return productMapper.upProduct(productId);
    }

    //根据产品id删除产品
    public void delProduct(Integer producttId) {
        Productt productt = producttDao.findById(producttId).get();
        productt.setProducttsc(1);
        producttDao.save(productt);
    }

    //根据产品id查询属性名
    public List<Attribute> selectchanpinshuxin(Integer productId) {
        return productMapper.selectchanpinshuxin(productId);
    }

    //查询类别是否被产品使用
    public List<Product> selectProCategory(Integer categoryId) {
        return productMapper.selectProCategory(categoryId);
    }

    //产品新增判断
    public List<Productt> selectProAtv(String productName,Integer producttId) {
        return productMapper.selectProAtv(productName,producttId);
    }

    //产品新增判断
    public List<Attribute> selectProAtName(Integer categoryId) {
        return productMapper.selectProAtName(categoryId);
    }

    //产品新增判断
    public List<AttributeValue> selectAtvName(Integer categoryId) {
        return productMapper.selectAtvName(categoryId);
    }

    //产品筛选分页
    public List<Productt> selectScreen(String search, String categoryName, String price1, String price2) {
        return productMapper.selectScreen(search,categoryName,price1,price2);
    }
}
